import instance from './axiosInstance.js'
import baseDomain from './config.js';

// 用户登录
exports.login = (data) => {
  console.log(data);
  var formData = new FormData()
  for (const key in data) {
    formData.append(key, data[key])
  }
  return instance.post('/api/user/login', formData);
}

// 用户注册
exports.register = (data) => {
  return instance.post('/api/user/register', data);
}

exports.queryPostsInfo = (order, from, to) => {
  let formData = new FormData();
  formData.append("from", from);
  formData.append("to", to);
  if(order == 'popular')
    formData.append("sortby", order);
  return instance.post('/api/talk/list', formData);
}

exports.getCaptchaAddress = () => {
  return baseDomain + '/api/captcha';
}

exports.getUploadProfileAddress = () => {
  return baseDomain + '/api/user/upload';
}

// 获取用户信息
exports.getUserInfo = () => {
  return instance.get('/api/userinfo/detail');
}

exports.uploadAPost = (formData) => {
  return instance.post('/api/talk/add', formData);
}


// // 退出登录
exports.logout = () => {
  return instance.get('/api/user/logout');
}

// 查询所有帖子
exports.queryAllTopics = () => {
  return instance.post('/api/talk/list');
}

// 发帖
exports.addTopic = (data) => {
  return instance.post('api/talk/add', data);
}

// 更新用户信息
exports.updateUserInfo = (data) => {
  console.log(data)
  return instance.post('/api/userinfo/update', data);
}

// 获取帖子详细信息
exports.getTopicInfo = (data) => {
  return instance.get('/api/talk/info', {
    params: {
      talkId: data,
    }
  });
}

// 获取一个帖子的所有评论
exports.getAllComment = (data) => {
  return instance.get('/api/comment/list', {
    params: {
      talkId: data,
    }
  })
}

// 评论
exports.addComment = (data) => {
  return instance.post('/api/comment/add', {
    talkId: data.talkId,
    content: data.content,
    create_datetime: data.create_datetime
  });
}

// 评论
exports.like = (type, id) => {
  return instance.post('/api/' + type + '/thumb/add', {
    _comment_talk_id: id,
  });
}


// 评论
exports.dislike = (type, id) => {
  return instance.post('/api/' + type + '/thumb/del', {
    _comment_talk_id: id,
  });
}

// 搜索
exports.search = (searchKey, from, to) => {
  let fata = new FormData();
  fata.append("key", searchKey);
  fata.append("from_param", from);
  fata.append("to_param", to);
  return instance.post('/api/search/default', fata);
}

// exports.getCommentInfo = (data) => {
//   return instance.get('/api/talk/info', {
//     params: {
//       talkId: data,
//     }
//   });
// }